/*
Copyright 2007 Nathan Baker

This file is part of XAliens.

XAliens is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

XAliens is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with XAliens.  If not, see <http://www.gnu.org/licenses/>.
 */

package sprites;

import exceptions.ImageNotLoadedException;

import java.awt.*;
import java.awt.image.BufferedImage;

public class Sprite {
    protected Integer mImageX, mImageY;
    protected Integer mImageW, mImageH;
    protected BufferedImage mImage;

    /**
     * Create a new sprite based on an image.
     *
     * @param img The image that is this sprite.
     */
    protected Sprite(BufferedImage img) {
        try {
            mImageW = img.getWidth(null);
            mImageH = img.getHeight(null);
            mImage = img;
        } catch (NullPointerException e) {
            System.err.print("\nCould not load image.\n" + e.getMessage());
        } catch (Exception e) {
            System.err.print("\nCould not load image: " + img.toString()
                    + "\nMessage was: " + e.getMessage());
        }
    }

    /**
     * Creates a Sprite instance without an image reference.
     */
    protected Sprite() {
        // this constructor doesn't do anything
    }

    /**
     * Get the width of the drawn sprite.
     *
     * @return The width in pixels of this sprite.
     */
    public int getWidth() {
        return mImageW;
    }

    /**
     * Returns true if the image associated with this Sprite has been loaded.
     *
     * @return True if mImage is !null
     */
    public Boolean imageLoaded() {
        return mImage != null;
    }

    /**
     * Get the height of the drawn sprite.
     *
     * @return The height in pixels of this sprite.
     */
    public int getHeight() {
        return mImageH;
    }

    /**
     * Returns the current X coordinate of the image.
     *
     * @return Current X coordinate.
     */
    public int getX() {
        return mImageX;
    }

    /**
     * Returns the current Y coordinate of the image.
     *
     * @return Current Y coordinate.
     */
    public int getY() {
        return mImageY;
    }

    /**
     * Returns the right coordinate (x + width) of the image.
     *
     * @return The right coordinate of the image.
     */
    protected int getRight() {
        return getX() + getWidth();
    }

    /**
     * Returns the bottom coordinate (y + height) of the image.
     *
     * @return The bottom coordinate of the image.
     */
    protected int getBottom() {
        return getY() + getHeight();
    }

    /**
     * Sets the X location of the sprite.
     *
     * @param x The X location of the sprite to set.
     */
    protected void setX(int x) {
        mImageX = x;
    }

    /**
     * Sets the Y location of the sprite.
     *
     * @param y The Y location of the sprite to set.
     */
    protected void setY(int y) {
        mImageY = y;
    }

    /**
     * @param x X location to set.
     * @param y Y location to set.
     * @return Always returns true;
     */
    protected void setLocation(int x, int y) {
        mImageX = x;
        mImageY = y;
    }

    /**
     * Draws the image at the current location.
     *
     * @param g The graphics context on which to draw the sprite.
     * @throws ImageNotLoadedException
     */
    protected void draw(Graphics2D g) {
        g.drawImage(mImage, mImageX, mImageY, null);
    }

    /**
     * Draw the sprite onto the graphics context provided
     *
     * @param g The graphics context on which to draw the sprite.
     * @param x The x location at which to draw the sprite.
     * @param y The y location at which to draw the sprite.
     */
    protected void draw(Graphics2D g, int x, int y) {
        mImageX = x;
        mImageY = y;
        g.drawImage(mImage, x, y, null);
    }
}
